﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;

namespace DARE
{// NOT DONE YET, SOME PROBLEMS WITH THE WIDGET THAT HAVE TO BE PARTIALLY HIDDEN
    /// <summary>
    /// [Deprecated][Not usable][Not commented]
    /// </summary>
    [AttrTypeDependentSerializable(typeof(CScrollablePaneData))]
    public class CScrollablePane : CWidget
    {
        #region fields

        private CVerticalScrollBar m_vScrollbar;
        private bool m_verticalScrollbarEnabled;
        private float m_posVerticalScrollbar;

        private CHorizontalScrollBar m_hScrollbar;
        private bool m_horizontalScrollbarEnabled;
        private float m_posHorizontalScrollbar;

        private Vector2 m_totalSize;
        private int m_nbelems;

        #endregion

        #region properties

        public override Vector2 Size
        {
            get
            {
                return base.Size;
            }
            set
            {
                base.Size = value;
                Actualize();
            }
        }

        public override Vector2 Position
        {
            get
            {
                return base.Position;
            }
            set
            {
                base.Position = value;
                Actualize();
                InitialPosition = new Vector2(m_rect.X, m_rect.Y);
            }
        }

        public override Rectangle Rect
        {
            get
            {
                return base.Rect;
            }
            set
            {
                base.Rect = value;
                Actualize();
                InitialPosition = new Vector2(m_rect.X, m_rect.Y);
            }
        }

        #endregion

        #region ctor

        public CScrollablePane(CHUDSystem hud, CWidget parent, string name)
            : base(hud, parent, name)
        {
            InitialPosition = new Vector2(m_rect.X, m_rect.Y);
            m_vScrollbar = new CVerticalScrollBar(hud, this, name + "_vertical scrollbar");
            m_vScrollbar.Size = new Vector2(15, m_rect.Height - 15);
            m_vScrollbar.Position = new Vector2(m_rect.Width - 15, 0);
            m_verticalScrollbarEnabled = true;

            m_hScrollbar = new CHorizontalScrollBar(hud, this, name + "_horizontal scrollbar");
            m_hScrollbar.Size = new Vector2(m_rect.Width - 15, 15);
            m_hScrollbar.Position = new Vector2(0, m_rect.Height - 15);
            m_horizontalScrollbarEnabled = true;

            m_posHorizontalScrollbar = m_hScrollbar.Value;
            m_posVerticalScrollbar = m_vScrollbar.Value;

            m_totalSize = Vector2.Zero;
            m_nbelems = 0;
        }

        #endregion

        #region actualize

        public override void Actualize()
        {
            base.Actualize();

            if (m_verticalScrollbarEnabled)
            {
                Rectangle vsr = m_vScrollbar.Rect;
                vsr.Width = 15;
                vsr.X = m_rect.X + m_rect.Width - vsr.Width;
                vsr.Y = m_rect.Y;
                vsr.Height = m_rect.Height - 15;
                m_vScrollbar.Rect = vsr;
            }
            if (m_horizontalScrollbarEnabled)
            {
                Rectangle hsr = m_hScrollbar.Rect;
                hsr.Width = m_rect.Width - 15;
                hsr.Height = 15;
                hsr.X = m_rect.X;
                hsr.Y = m_rect.Y + m_rect.Height - hsr.Height;
                m_hScrollbar.Rect = hsr;
            }
        }

        #endregion

        #region giveSubRect

        public override void giveSubRect(ref Rectangle m_rectangle)
        {
            giveSubRect(ref m_rectangle, false);
        }

        public override void giveSubRect(ref Rectangle m_rectangle, bool force)
        {
            base.giveSubRect(ref m_rectangle, force);
         //   childrensChanged = true;
        }

        public override void giveSubRect(Vector2 pos, Vector2 size, ref Rectangle m_rectangle)
        {
            giveSubRect(pos, size, ref m_rectangle, false);
        }

        public override void giveSubRect(Vector2 pos, Vector2 size, ref Rectangle m_rectangle, bool force)
        {
            base.giveSubRect(pos, size, ref m_rectangle, force);
         //   childrensChanged = true;
        }
        #endregion


        #region update/draw
        bool childrensChanged = false;
        public override bool Update(GameTime gameTime)
        {
            if (!m_isVisible)
                return false;

            if (m_children.Values.Count - 2 != m_nbelems)
            {
                m_nbelems = m_children.Values.Count - 2;
                Actualize();
                childrensChanged = true;
            }
            if (childrensChanged)
            {
                float maxX = 1;
                float maxY = 1;
                foreach (CWidget child in m_children.Values)
                    if (child != m_vScrollbar && child != m_hScrollbar)
                    {
                        //float wid = child.InitialPosition.Value.X + child.Rect.Width - m_rect.Width + m_vScrollbar.Rect.Width;
                        //if (wid > maxX)
                        //    maxX = wid;
                        //float heig = child.InitialPosition.Value.Y + child.Rect.Height - m_rect.Height + m_hScrollbar.Rect.Height;
                        //if (heig > maxY)
                        //    maxY = heig;
                    }
                m_totalSize.X = maxX ;
                m_totalSize.Y = maxY;


                if (m_totalSize.Y > m_rect.Height - m_hScrollbar.Rect.Height)
                    maxY = ((m_rect.Height - m_hScrollbar.Rect.Height) / m_totalSize.Y);
                else
                    maxY = 1;
               // m_vScrollbar.SliderSize = new Vector2(m_vScrollbar.SliderSize.X, maxY * (m_vScrollbar.Rect.Height - 30));//foutr le 30 en pas dur.
                if (m_totalSize.X > m_rect.Width - m_vScrollbar.Rect.Width)
                    maxX = ((m_rect.Width - m_vScrollbar.Rect.Width) / m_totalSize.X);
                else
                    maxX = 1;
                //m_hScrollbar.SliderSize = new Vector2(maxX * (m_hScrollbar.Rect.Width - 30), m_hScrollbar.SliderSize.Y);
                //sets the incrementor of the value in the scrollbars to increment pixels by 10 (every click offsets 10px)
                m_hScrollbar.Incrementation = 10 * 100 / (m_hScrollbar.Rect.Width - 30);
                m_vScrollbar.Incrementation = 10 * 100 / (m_vScrollbar.Rect.Height - 30);
                Actualize();
                childrensChanged = false;
            }
            base.Update(gameTime);
            if (m_verticalScrollbarEnabled && m_posVerticalScrollbar != m_vScrollbar.Value)
            {
                float translated = (m_vScrollbar.Value - m_posVerticalScrollbar) * m_totalSize.Y / 100;
                foreach (CWidget child in m_children.Values)
                    if (child != m_vScrollbar && child != m_hScrollbar)
                    {
                        float x = child.Position.X;
                        float y = child.Position.Y - translated;
                        child.Position = new Vector2(x, y);
                    }
                Actualize();
                m_posVerticalScrollbar = m_vScrollbar.Value;
            }
            if (m_horizontalScrollbarEnabled && m_posHorizontalScrollbar != m_hScrollbar.Value)
            {
                float translated = (m_hScrollbar.Value - m_posHorizontalScrollbar) * m_totalSize.X / 100;
                foreach (CWidget child in m_children.Values)
                    if (child != m_vScrollbar && child != m_hScrollbar)
                    {
                        float x = child.Position.X - translated;
                        float y = child.Position.Y;
                        child.Position = new Vector2(x, y);
                    }
                Actualize();
                m_posHorizontalScrollbar = m_hScrollbar.Value;
            }

            return true;
        }

        public override bool Draw(GameTime gameTime)
        {
            if (!m_isVisible)
                return false;

            if (m_texture != null)
                m_texture.Draw(gameTime);
                //m_spriteBatch.Draw(m_texture, new Rectangle(m_rect.X, m_rect.Y, m_rect.Width - m_vScrollbar.Rect.Width, m_rect.Height - m_hScrollbar.Rect.Height), m_color);
            foreach (CWidget child in m_children.Values)
            {

                if (m_rect.Contains(child.Rect))
                    child.Draw(gameTime);
                else// if (m_rect.Intersects(child.Rect))
                {

                    if (child.Texture != null)
                    {
                        //Rectangle tmp = child.Rect;
                        int diffx = (m_rect.X - child.Rect.X);
                        int diffY = m_rect.Y - child.Rect.Y;
                        child.Rect = new Rectangle(child.Rect.X + diffx, child.Rect.Y + diffY, child.Rect.Width - diffx, child.Rect.Height - diffY);
                        child.Draw(gameTime);
                        //child.Rect = tmp;
                    }
                }
                //   child.Rect = tmpRect;
            }
            return true;
        }

        #endregion

    }
}
